{include file="public/header" /}
<body class="gray-bg">
<div class="layui-fluid layui-col-md12">
    <div class="layui-card">
        <div class="layui-card-header">添加文章</div>
        <div class="layui-card-body layui-form">
            <div class="layui-row layui-col-space10 layui-form-item ">
<!--                <div class="layui-form-item layui-col-md-offset1 layui-col-md8">-->
<!--                    <label class="layui-form-label">文章标签</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" class="layui-input layui-hide" id="layTables" name="label"  value="" lay-verify="required" placeholder="文章标题">-->
<!--                    </div>-->
<!--                </div>-->
                <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                    <label class="layui-form-label">文章标签</label>
                    <div class="layui-input-block">
                        <select name="label" xm-select="label" lay-verify="required" xm-select-search="{:url('Article/get_formselectlist')}" xm-select-create="">
                            {if !empty($info)}
                            {foreach name="info" item="vo"}
                            <option>{$vo.name}</option>
                            {/foreach}
                            {/if}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                    <label class="layui-form-label">一键粘贴</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input layui-disabled" lay-verify="required" placeholder="一键粘贴" value="360VR全景" readonly>
                        <div class="layui-copy" onclick="wk.lay_copy(this)" title="复制" alt="复制"><span><i class="fa fa-paste"></i></span></div>
                    </div>
                </div>
                <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                    <label class="layui-form-label">关键字</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input " name="keyword" lay-verify="required" placeholder="文章关键字">
                    </div>
                </div>
                <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                    <label class="layui-form-label">文章标题</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input " name="title" lay-verify="required" placeholder="文章标题">
                    </div>
                </div>
                <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                    <label class="layui-form-label">所属分类</label>
                    <div class="layui-input-block">
                        <select name="cate_id" lay-verify="required" lay-search="">
                            <option value="">请选择分类</option>
                            {if !empty($cate)}
                                {foreach name="cate" item="vo"}
                                    <option value="{$vo.id}">{$vo.name}</option>
                                {/foreach}
                            {/if}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                    <label class="layui-form-label">描述</label>
                    <div class="layui-input-block">
                        <textarea name="remark" class="layui-textarea" lay-verify="required" placeholder="文章描述"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                    <label class="layui-form-label">封面图片上传</label>
                    <input type="hidden" name="photo" id="lay-img" lay-verify="image">
                    <div class="layui-input-block">
                        <div id="lay-upload">上传图片</div>
                        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                            预览图：
                            <div class="layui-upload-list" id="lay-img-list">
                            </div>
                        </blockquote>
                    </div>
                </div>
                <div class="layui-form-item layui-col-md-offset1 layui-col-md10">
                    <label class="layui-form-label">文章内容</label>
                    <div class="layui-input-block" >
                        <textarea class="layui-textarea" name="content" lay-verify="content" id="LAY_editor" placeholder="文章内容"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                    <label class="layui-form-label">作者</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input " name="writer" lay-verify="required" placeholder="文章作者">
                    </div>
                </div>
                <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                    <label class="layui-form-label">是否推荐</label>
                    <div class="layui-input-block">
                        <input type="radio" name="is_tui" value="1" title="是" checked>
                        <input type="radio" name="is_tui" value="2" title="否" >
                    </div>
                </div>
                <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="component-form-element">保存
                        </button>
                        <button class="layui-btn layui-btn-primary" onclick="history.back()">关闭</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script>
    layui.config({
        base: '/src/modules/' //静态资源所在路径
    }).extend({
        formSelects: 'formSelects-v4'
    }).use(['formSelects']),function(){
        var formSelects = layui.formSelects
    };

    //标签
    wk.tableSelect({elem:'#layTables',field:'id',name:'name',key:'keyword',splaceholder:'昵称',iplaceholder:'请选择数据',url:"{:url('getUserData')}",cols:[{type: 'checkbox'}, {field: 'id', width: '', title: 'ID', align: 'center'}, {field: 'name', width: '', title: '用户名', align: 'center'}]});

    // layedit富文本编辑器
    var index = wk.layeditor('LAY_editor');

    // 图片上传
    if("{$configState}" == 0){
        wk.uploadImg({size:2,type:'jpg,jpeg,png',url:"{:url('Upload/upload')}",domain:"{:config('DOMAIN')}"});
    }else {
        wk.uploadImg({size:2,type:'jpg,jpeg,png',url:"{:url('admin/upload/uploadLocality')}"});
    }

    layui.use(['form','upload','layedit','element','formSelects'], function() {
        var form = layui.form
            , layedit = layui.layedit
            , upload = layui.upload
            , element = layui.element
            , formSelects = layui.formSelects


        //layedit富文本
        layedit.set({
            uploadImage: {
                url: '/admin/Upload/layUpload',
                type: 'post'
            }
        });

        var index = layedit.build('LAY_editor', {height: 400});

        form.verify({
            image:[/\S/,'请上传图片'],
            content:function(value,item){
                if(layedit.getContent(index) == ''){
                    return '请填写文章内容';
                }
            }
        });

        form.on('submit(component-form-element)', function (data) {
            $('.layui-btn').addClass('layui-disabled').attr('disabled','disabled');
            data.field.content = layedit.getContent(index);
            data.field.selectlist = formSelects.value('label', 'all');
            $.ajax({
                url:"{:url('add_article')}",
                type:'post',
                dataType:'json',
                data:data.field,
                success:function(res){
                    if (res.code == 200) {
                        layer.msg(res.msg,{icon:1,time:1500,shade:0.1},function(index){
                            setTimeout(function () {
                                wk.layer_close();
                                window.location.href="{:url('Article/index')}";
                            },100);
                        })
                    } else {
                        $(".layui-btn").removeClass('layui-disabled').removeAttr('disabled');
                        wk.error(res.msg);
                        return false;
                    }
                }
            })
        });
    });
</script>
</body>
</html>